package com.wuming.house.controller;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.core.WumingRole;
import com.wuming.house.model.House;
import com.wuming.house.model.Member;
import com.wuming.house.model.Secondhouse;
import com.wuming.house.service.GroupHouseService;
import com.wuming.house.service.HouseService;
import com.wuming.house.service.MemberService;
import com.wuming.house.service.SecondhouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/secondhouse")
public class SecondhouseController extends BaseController {

    @Autowired
    private SecondhouseService secondhouseService;
    @Autowired
    private GroupHouseService groupHouseService;
    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd(@RequestParam Integer memberId,Model model) {
        model.addAttribute("memberId",memberId);
        return "/secondhouse/editSecondHouse";
    }

    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult save(Secondhouse house) {
        try {
            if (null == house.getId()) {
                house.setPublishTime(new Date());
                secondhouseService.saveSecondhouse(house);
            } else {
                secondhouseService.modifySecondhouse(house);
            }
            return new JsonResult<Object>(house, "保存成功.", true);
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return new JsonResult<Object>(null, "保存失败,请联系系统管理员.", false);
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return new JsonResult<Object>(null, "保存失败,请联系系统管理员.", false);
        }
    }


    @RequestMapping(value = "/toManageSecondHouse", method = RequestMethod.GET)
    public String toManageHouse() {
        return "/secondhouse/manageSecondHouse";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam Integer memberId,
                             @RequestParam(required = false) String name, @RequestParam(defaultValue = "1") int pageIndex
            , @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Member member = memberService.getMemberById(memberId);
            if (member.getRole()== WumingRole.WUMING_USERCENTER_ROLE_ADMINISTRATOR.ordinal()){
                List<Secondhouse> houses = secondhouseService.getSecondhouseByPage(name, pageIndex, pageSize);
                int total = secondhouseService.getTotal(name);
                return CommonResult.createSuccessResult(houses, total, "查询楼盘成功.");
            }
            //非超级管理员登录
            List<Secondhouse> houses = secondhouseService.getSecondhouseByMember(memberId,name,pageIndex,pageSize);
            int total = secondhouseService.getTotalByMember(memberId,name);
            return CommonResult.createSuccessResult(houses,total,"查询楼盘成功");
        } catch (BusinessException be) {
            logger.error("查询楼盘失败.异常:{}", be.getMessage());
            return CommonResult.createFailureResult("查询楼盘失败");
        } catch (Exception e) {
            logger.error("查询楼盘失败.异常:{}", e.getMessage());
            return CommonResult.createFailureResult("查询楼盘失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult detele(@PathVariable Long id) {
        logger.info("准备删除楼盘信息ID:{}", id);
        try {
            //删除二手房分组中的存在该楼盘的数据
            groupHouseService.deleteByHouseId(id);
            secondhouseService.removeSecondhouseById(id);
            return CommonResult.createSuccessResult("删除楼盘信息成功");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除楼盘信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除楼盘信息失败.");
        }
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String toEdit(@PathVariable Long id, Model model) {
        logger.info("准备修改楼盘信息ID:{}", id);
        Secondhouse house = secondhouseService.getSecondhouseById(id);
        model.addAttribute("house", house);
        return "/secondhouse/editSecondHouse";
    }

    @RequestMapping(value = "/manageImg/{houseId}", method = RequestMethod.GET)
    public String manageImg(@PathVariable Long houseId, @RequestParam Integer houseType, Model model) {
        model.addAttribute("houseType", houseType);
        model.addAttribute("houseId", houseId);
        return "/house/manageImg";
    }

    @RequestMapping(value = "/manageTag/{houseId}", method = RequestMethod.GET)
    public String manageTag(@PathVariable Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return "/secondhouse/manageTag";
    }

}
